// pages/serve/ranking/ranking.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bar_Height: wx.getSystemInfoSync().statusBarHeight + 45,
    title:1,//头部切换
    gradeMyArr:[],//我的所有成绩
    highest:0,//我的最高分
    myParticulars:{},//我的信息
    ranks:[],//排行榜
    myRanks:{},//自己的排名
    gif: "true",

  },
  //模式选择
  chapterTitle(e) {
    var that = this
    // console.log(e.currentTarget.dataset.index)
    if (e.currentTarget.dataset.index == 1) {
      gradeMy(that,2)
      gradeMyBest(that,2)
    }
    if (e.currentTarget.dataset.index == 2) {
      rankBest(that,2)
    }
    that.setData({
      title: e.currentTarget.dataset.index
    })
  },
  //去考试
  examClick(e) {
    wx.navigateTo({
      url: '/pages/serve/exam/exam',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    gradeMy(that,2)
    gradeMyBest(that,2)
    that.setData({
      myParticulars:wx.getStorageSync("myParticulars")
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (this.title == 1) {
      gradeMy(this, 1)
      gradeMyBest(this, 1)
    }
    if (this.title == 2) {
      rankBest(this, 1)
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

//我的成绩
function gradeMy(that,num) {
  // console.log(course_id, status, choice)
  wx.request({
    url: 'https://minitest.17jxyx.com/api/v1/analog/log/' + app.titleStudy + '/' + app.cardType ,
    method: "GET",
    header: {
      token: app.token
    },
    success: function (res) {
      // console.log(res)
      // if (num == 1) {
      //   wx.hideNavigationBarLoading() //完成停止加载
      //   wx.stopPullDownRefresh() //停止下拉刷新
      // }
      // console.log(res.data[3])
      if(res.data.length > 0){
        that.setData({
          gradeMyArr: res.data
        })
      }
    },
    fail: function (res) {
      console.log(res)
    }
  })
}
//我的最好成绩
function gradeMyBest(that, num) {
  // console.log(course_id, status, choice)
  wx.request({
    url: 'https://minitest.17jxyx.com/api/v1/analog/log/max/' + app.titleStudy + '/' + app.cardType,
    method: "GET",
    header: {
      token: app.token
    },
    success: function (res) {
      // console.log(res)
      if (num == 1) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
      // console.log(res.record)-
      // if (res.data.length > 0) {
        that.setData({
          gif: "false",
          highest: res.data.record
        })
      // }
    },
    fail: function (res) {
      console.log(res)
    }
  })
}
//排行榜
function rankBest(that, num) {
  // console.log(course_id, status, choice)
  wx.request({
    url: 'https://minitest.17jxyx.com/api/v1/analog/log/rank/' + app.titleStudy + '/' + app.cardType,
    method: "GET",
    header: {
      token: app.token
    },
    success: function (res) {
      // console.log(res)
      // console.log(res.data)
      if (num == 1) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
      that.setData({
        ranks: res.data.rank,
        gif: "false",
        myRanks: res.data.user
      })
    },
    fail: function (res) {
      console.log(res)
    }
  })
}